package com.baidu.shop.service.impl;

import com.baidu.shop.base.BaseApiService;
import com.baidu.shop.base.Result;
import com.baidu.shop.dto.BrandDTO;
import com.baidu.shop.entity.BrandEntity;
import com.baidu.shop.entity.CategoryBrandEntity;
import com.baidu.shop.entity.CategoryEntity;
import com.baidu.shop.entity.SpuEntity;
import com.baidu.shop.mapper.BrandMapper;
import com.baidu.shop.mapper.CategoryBrandMapper;
import com.baidu.shop.mapper.SpuMapper;
import com.baidu.shop.service.BrandService;
import com.baidu.shop.utils.BaiduBeanUtil;
import com.baidu.shop.utils.ObjectUtil;
import com.baidu.shop.utils.PinyinUtil;
import com.baidu.shop.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @ClassName: BrandServiceImpl
 * @Author: User
 * @date: 2020.08.31 14:46
 * @version: 1.0
 */
@RestController
@Slf4j
public class BrandServiceImpl extends BaseApiService implements BrandService {

    @Resource
    private BrandMapper mapper;
    @Resource
    private CategoryBrandMapper cbmapper;
    @Resource
    private SpuMapper spuMapper;


    @Override
    public Result<List<BrandEntity>> getBrandByIdList(String brandIds) {
        List<Integer> brandIntIds = Arrays.asList(brandIds.split(",")).stream().map(BrandId -> {
            return Integer.parseInt(BrandId);
        }).collect(Collectors.toList());
        List<BrandEntity> brandEntities = mapper.selectByIdList(brandIntIds);
        return this.setResultSuccess(brandEntities);
    }

    @Override
    public Result getBrandByCategory(Integer cid) {
        List<BrandEntity> list = mapper.getBrandByCategory(cid);
        return this.setResultSuccess(list);
    }

    @Override
    public Result getCategory(Integer id) {
        List<CategoryEntity> list = cbmapper.getCategory(id);

        return this.setResultSuccess(list);
    }

    @Override
    public Result editBrand(BrandDTO brandDTO) {
        //获取名称  首字母或拼音的首字母
        char c = brandDTO.getName().charAt(0);
        String upperCase = PinyinUtil.getUpperCase(String.valueOf(c), PinyinUtil.TO_FIRST_CHAR_PINYIN);
        BrandEntity brandEntity = BaiduBeanUtil.copyProperties(brandDTO, BrandEntity.class);
        brandEntity.setLetter(upperCase.charAt(0));
        mapper.updateByPrimaryKeySelective(brandEntity);
        //删除中间表信息
        Example example = new Example(CategoryBrandEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("brandId",brandDTO.getId());
        cbmapper.deleteByExample(example);

        //遍历品牌类型 放到中间实体类 组合成list 来批量新增
        addCategoryBrand(brandDTO, brandEntity);

        return this.setResultSuccess();
    }

    @Override
    public Result delBrand(Integer id) {
        String msg = "";
        //是否被商品绑定
        Example spuExample = new Example(SpuEntity.class);
        spuExample.createCriteria().andEqualTo("brandId",id);
        List<SpuEntity> spuEntities = spuMapper.selectByExample(spuExample);
        if (!spuEntities.isEmpty()){
            msg+="品牌被["+spuEntities.get(0).getTitle()+"]....等、"+spuEntities.size()+"个商品绑定,无法被删除!!!";
        }
        //判断错误提示长度是否为0
        if (msg.trim().length()>0) return this.setResultError(msg);

        //删除中间表信息
        Example example = new Example(CategoryBrandEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("brandId",id);
        cbmapper.deleteByExample(example);
        //删除
        mapper.deleteByPrimaryKey(id);
        return this.setResultSuccess();
    }

    @Override
    public Result<PageInfo<BrandEntity>> getBrand(BrandDTO brandDTO) {
        //分页
        PageHelper.startPage(brandDTO.getPage(), brandDTO.getPagesize());
        //排序
        Example example = new Example(BrandEntity.class);
       if (StringUtil.isNotEmpty(brandDTO.getSort()))example.setOrderByClause(brandDTO.getOrderByClause());
        //条件
         if(StringUtil.isNotEmpty(brandDTO.getName())) example.createCriteria()
                 .andLike("name","%"+brandDTO.getName()+"%");
        //查询
        List<BrandEntity> brandEntities = mapper.selectByExample(example);
        //获取page信息
        PageInfo<BrandEntity> brandEntityPageInfo = new PageInfo<BrandEntity>(brandEntities);
        return this.setResultSuccess(brandEntityPageInfo);
    }

    @Override
    public Result saveBrand(BrandDTO brandDTO) {
          if (ObjectUtil.isNull(brandDTO)){
              return this.setResultError(400, "值为空");
          }
          //获取名称  首字母或拼音的首字母
        char c = brandDTO.getName().charAt(0);
        String upperCase = PinyinUtil.getUpperCase(String.valueOf(c), PinyinUtil.TO_FIRST_CHAR_PINYIN);
        BrandEntity brandEntity = BaiduBeanUtil.copyProperties(brandDTO, BrandEntity.class);
        brandEntity.setLetter(upperCase.charAt(0));
        mapper.insertSelective(brandEntity);

           if (StringUtil.isEmpty(brandDTO.getCategory())){
               return this.setResultError("品牌类型不能为空");
           }
          //遍历品牌类型 放到中间实体类 组合成list 来批量新增
        addCategoryBrand(brandDTO, brandEntity);
        return this.setResultSuccess();
    }

    private void addCategoryBrand(BrandDTO brandDTO, BrandEntity brandEntity) {
        if(brandDTO.getCategory().contains(",")){
            List<CategoryBrandEntity> list = Arrays.asList(brandDTO.getCategory().split(",")).stream().map(str -> {
                CategoryBrandEntity CBEntity = new CategoryBrandEntity();
                CBEntity.setBrandId(brandEntity.getId());
                CBEntity.setCategoryId(StringUtil.getInteger(str));
                return CBEntity;
            }).collect(Collectors.toList());
            cbmapper.insertList(list);
        }else{
            CategoryBrandEntity CBEntity = new CategoryBrandEntity();
            CBEntity.setBrandId(brandEntity.getId());
            CBEntity.setCategoryId(StringUtil.getInteger(brandDTO.getCategory()));
            cbmapper.insertSelective(CBEntity);
        }
    }
}
